package com.jihox.abookpro.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.FragmentManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.jihox.abookpro.R;
import com.jihox.abookpro.fragment.AssertFragment;
import com.jihox.abookpro.util.AndroidImageFile;
import com.jihox.abookpro.util.NativeImageLoader;
import com.jihox.abookpro.util.NativeImageLoader.NativeImageCallBack;
import com.jihox.abookpro.view.ShowPhotoImageView;
import com.jihox.abookpro.view.ViewHolder;
import com.jihox.abookpro.view.ShowPhotoImageView.OnMeasureListener;

public class PhotoChildAdapter extends BaseAdapter {
	private Point mPoint = new Point(0, 0);//用来封装ImageView的宽和高的对象
	/**
	 * 用来存储图片的选中情况
	 */

	private HashMap<Integer, Boolean> mSelectMap = new HashMap<Integer, Boolean>();
	private GridView mGridView;
	private List<AndroidImageFile> albumPhotoList = new ArrayList<AndroidImageFile>();
	private String folderPath="";
	protected LayoutInflater mInflater;
	private List<AndroidImageFile> selectedList;
	private Context context;
	private ConvertViewOnClickListener convertViewOnClickListener;
	
	public PhotoChildAdapter(Context context, List<AndroidImageFile> folderPhotoList, ArrayList<AndroidImageFile> selectedPhotoList, GridView mGridView) {
		if(folderPhotoList!=null)
			this.albumPhotoList = folderPhotoList;
		this.mGridView = mGridView;
		this.context = context;
		this.selectedList = selectedPhotoList;
		mInflater = LayoutInflater.from(context);
	}

	public void setConvertViewOnClickListener(
			ConvertViewOnClickListener convertViewOnClickListener) {
		this.convertViewOnClickListener = convertViewOnClickListener;
	}

	@Override
	public int getCount() {
		return albumPhotoList.size();
	}

	@Override
	public Object getItem(int position) {
		return albumPhotoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder;
		final AndroidImageFile aif = albumPhotoList.get(position);

		if(convertView == null){
			convertView = mInflater.inflate(R.layout.photo_child_item, null);
			viewHolder = new ViewHolder();
			viewHolder.mImageView = (ShowPhotoImageView) convertView.findViewById(R.id.child_image);
			viewHolder.mCheckBox = (CheckBox) convertView.findViewById(R.id.child_checkbox);
			viewHolder.mTextView = (TextView) convertView.findViewById(R.id.child_number);
			viewHolder.mHintTextView = (TextView) convertView.findViewById(R.id.child_number_hint);
			//用来监听ImageView的宽和高
			viewHolder.mImageView.setOnMeasureListener(new OnMeasureListener() {

				@Override
				public void onMeasureSize(int width, int height) {
					mPoint.set(width, height);
				}
			});
			convertView.setTag(viewHolder);
		}else{
			viewHolder = (ViewHolder) convertView.getTag();
			viewHolder.mImageView.setImageResource(R.drawable.pictures_no);
		}
		viewHolder.mImageView.setTag(aif);
		viewHolder.mCheckBox.setTag(aif+"CheckBox");
		//		viewHolder.mImageView.setOnClickListener(new OnClickListener() {
		//			@Override
		//			public void onClick(View arg0) {
		//				// TODO Auto-generated method stub
		//				viewHolder.mCheckBox.performClick();
		//			}
		//		});
		//
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				convertViewOnClickListener.onClick(viewHolder,position,aif);
			}
		});

		viewHolder.mCheckBox.setChecked(mSelectMap.containsKey(position) ? mSelectMap.get(position) : false);

		//利用NativeImageLoader类加载本地图片
		Bitmap bitmap = NativeImageLoader.getInstance().loadNativeImage(aif, mPoint,context,new NativeImageCallBack() {
			@Override
			public void onImageLoader(Bitmap bitmap, AndroidImageFile aif) {
				ImageView mImageView = (ImageView) mGridView.findViewWithTag(aif);
				CheckBox mCheckBox = (CheckBox) mGridView.findViewWithTag(aif+"CheckBox");
				if(bitmap != null && mImageView != null&& mCheckBox!=null){
					mImageView.setImageBitmap(bitmap);
					Drawable d = mImageView.getDrawable();//This is available from API level 1.setImageDrawable need 16 4.1
					if(d!=null)
						d.setAlpha(mCheckBox.isChecked()?100:255);
					mImageView.setImageDrawable(d);
				}
			}
		});

		if(bitmap != null){
			viewHolder.mImageView.setImageBitmap(bitmap);
		}else{
			viewHolder.mImageView.setImageResource(R.drawable.pictures_no);
		}

		if(viewHolder.mCheckBox.isChecked()){
			int num = getSequenceInSelectedList(albumPhotoList.get(position).getPath());
			if(num==0){
				viewHolder.mTextView.setText(context.getResources().getString(R.string.T_Cover_Page_Text));//加序号~~~
			}else{
				viewHolder.mTextView.setText("" + num);//加序号~~~
			}
			Drawable d = viewHolder.mImageView.getDrawable();//This is available from API level 1.setImageDrawable need 16 4.1
			d.setAlpha(100);
			viewHolder.mImageView.setImageDrawable(d);
			//			viewHolder.mImageView.setImageAlpha(100);
		}else{
			Drawable d = viewHolder.mImageView.getDrawable();//This is available from API level 1.setImageDrawable need 16
			d.setAlpha(255);
			viewHolder.mImageView.setImageDrawable(d);
			//			viewHolder.mImageView.setImageAlpha(255);
			viewHolder.mTextView.setText("");//加序号~~~
		}

		return convertView;
	}
	private int getSequenceInSelectedList(String path) {
		// TODO Auto-generated method stub
		int num = 0;
		for(num=0;num<selectedList.size();num++){
			if(selectedList.get(num).getPath().equals(path)){
				break;
			}
		}
		return num; 
	}

	public void userNotifyDataSetChanged() {//List<AndroidImageFile> selectedList
		//		this.selectedList = selectedList;
		mSelectMap.clear();
		if (albumPhotoList != null && albumPhotoList.size()>0){
			folderPath = albumPhotoList.get(0).getPath().substring(0, albumPhotoList.get(0).getPath().lastIndexOf("/"));
			for(int index = 0; index<selectedList.size();index++){//给相册中，以选中的照片初始化状态
				String specificFolder = selectedList.get(index).getPath().substring(0, selectedList.get(index).getPath().lastIndexOf("/"));
				if(specificFolder.equals(folderPath)){//首先判断是不是该相册的相片
					for(int indexx=0;indexx<albumPhotoList.size();indexx++){
						if(albumPhotoList.get(indexx).equals(selectedList.get(index))){
							mSelectMap.put(indexx, true);
							break;
						}
					}	
				}
			}
		}
		
		super.notifyDataSetChanged();
	}


	public HashMap<Integer, Boolean> getmSelectMap() {
		return mSelectMap;
	}
	public void setmSelectMap(HashMap<Integer, Boolean> mSelectMap) {
		this.mSelectMap = mSelectMap;
	}
	public List<AndroidImageFile> getAlbumPhotoList() {
		return albumPhotoList;
	}
	public interface ConvertViewOnClickListener{
		public void onClick(ViewHolder viewHolder,int position, AndroidImageFile aif);
	}
}
